

************** Spatial Regressions ******************
* spatial transformation completed previously in spgen_4_* 

************************************
* Contents
		* Radiant based: section 2 horizontal, 6 vertical, counter-clockwise.
		* 4950 cells based on k_bins of width 3 KwH 
		* Outcomes: sales and count, each in level, log+1, and ihs transformation
		* standard errors clustered by cell   
************************************

************************************
* Variables
		* cm: country-month items (second level identifier)
		* cell: 1-50 are bunchers at window = 2. (primary identifier)
		* a_bin/e_bin: individual attributes, unstandardized 
		* csd  / ccd: sales / product count in cell. 
		
		
capture log close 
clear all
set more off 
*set maxvar 30000, permanently // required to load matrix 
* ssc install reghdfe 
* ssc install ftools

* Set directory
global server "R:\WSV2\TBu_AKe\Spatial_NEW" 
cd "$server" // set working directory 

capture mkdir Wmat_Grid_test
global store "${server}\Wmat_Grid_test"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 


cd "$store"
 log using spatial_6_regs_s_grid_1702_vuong, replace   
 

			***************************
			* run regression: OLS, fe *
			***************************		

cd "C:\Users\hy65byfe\Desktop\smerge_0712\Grid_4950"	
*cd "R:\WSV2\TBu_AKe\Spatial_NEW\SCB_4950"	
use spatial_regs_grid_0111_monthly, replace  
			
			
*preserve
*spmatrix dir 
keep if year == 2014 // no changes. otherwise matrices are wrong in previous. 	
	
	
			*************************
			***** Single Matrix *****
			*************************	
		

** log and ihs transformations to the untransformed data **

		*** outcomes and polar cases (no q indexing needed, only double loop)
foreach varx of varlist csd L3_sd R_L3_sd H_L3_sd 		ccd L3_cd    R_L3_cd H_L3_cd {

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   quietly replace ihs_`varx'=log(ihs_`varx')
 }
 
		*** spatial variables *** 
forvalues q = 5(5)40 { 
	foreach varx of varlist 	W`q'_L3_sd O`q'_L3_sd 	W`q'_L3_cd O`q'_L3_cd {

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   quietly replace ihs_`varx'=log(ihs_`varx')
	}
}


			**********************************************
			***** 			Regressions 			 *****
			**********************************************

			
	***************** Baseline Vertical Regression ****************************


	reghdfe  log_csd    log_L3_sd 	log_R_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  residuals(log_V_R`q')
	estimates store log_V_R`q'
	gen ll_log_RV = (log_V_R`q')^2
	
	
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_R_L3_sd							if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  residuals(ihs_V_R`q')
	estimates store ihs_V_R`q'
	gen ll_ihs_RV = (ihs_V_R`q')^2
	
	estimates table *V*, b(%9.4f) stats(N r2 r2_a F ll) star(0.10 .05 .01) // vertical, column (6) in main tables 

	sum log_V* 
	sum ihs_V*
	
	
	******************** Nonnested Alternative **********************************
	
		** Results for log transformed data **
	
*quietly {
	
	
			*** Horizontal 
	
		reghdfe  log_csd    log_L3_sd 	log_H_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_H)
	estimates store log_H
	gen ll_log_H = (log_H)^2
	gen diff_log_H = ll_log_RV - ll_log_H
		
		

				 *** Northwest quadrant
				 
	forvalues q = 5(5)40 {
		reghdfe  log_csd    log_L3_sd 	log_W`q'_L3_sd						  if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_W`q')
	estimates store log_W`q'
	gen ll_log_W`q' = (log_W`q')^2
	gen diff_log_W`q' = ll_log_RV - ll_log_W`q'
		
	}

				 *** Northeast quadrant
				 
	forvalues q = 5(5)40 {
		reghdfe  log_csd    log_L3_sd 	log_O`q'_L3_sd						  if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_O`q')
	estimates store log_O`q'
	gen ll_log_O`q' = (log_O`q')^2
	gen diff_log_O`q' = ll_log_RV - ll_log_O`q'
		
	}

*}

				*** Constant-only regression 
				
	reg diff_log_H, vce(cluster cell)
	estimates store log_diff_H

forvalues q = 5(5)40 {
	
	reg diff_log_W`q', vce(cluster cell)
	estimates store log_diff_W`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
	
	reg diff_log_O`q', vce(cluster cell)
	estimates store log_diff_O`q'
} 

	
estimates table  log_diff_H log_diff_W* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  log_diff_H log_diff_W* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

estimates table  log_diff_H log_diff_O* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  log_diff_H log_diff_O* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

    ** Results for ihs transformed data **
	
*quietly {

			*** Horizontal 
	
		reghdfe  ihs_csd    ihs_L3_sd 	ihs_H_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_H)
	estimates store ihs_H
	gen ll_ihs_H = (ihs_H)^2
	gen diff_ihs_H = ll_ihs_RV - ll_ihs_H
		
		

				 *** Northwest quadrant
				 
	forvalues q = 5(5)40 {
		reghdfe  ihs_csd    ihs_L3_sd 	ihs_W`q'_L3_sd						  if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_W`q')
	estimates store ihs_W`q'
	gen ll_ihs_W`q' = (ihs_W`q')^2
	gen diff_ihs_W`q' = ll_ihs_RV - ll_ihs_W`q'
		
	}

				 *** Northeast quadrant
				 
	forvalues q = 5(5)40 {
		reghdfe  ihs_csd    ihs_L3_sd 	ihs_O`q'_L3_sd						  if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_O`q')
	estimates store ihs_O`q'
	gen ll_ihs_O`q' = (ihs_O`q')^2
	gen diff_ihs_O`q' = ll_ihs_RV - ll_ihs_O`q'
		
	}

*}

				*** Constant-only regression 
				
	reg diff_ihs_H, vce(cluster cell)
	estimates store ihs_diff_H

forvalues q = 5(5)40 {
	
	reg diff_ihs_W`q', vce(cluster cell)
	estimates store ihs_diff_W`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
	
	reg diff_ihs_O`q', vce(cluster cell)
	estimates store ihs_diff_O`q'
} 

	
estimates table  ihs_diff_H ihs_diff_W* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  ihs_diff_H ihs_diff_W* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

estimates table  ihs_diff_H ihs_diff_O* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  ihs_diff_H ihs_diff_O* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)
	
capture log close 

cd "$store"
log using results_summary_s_grid_1702_vuong, replace 	
	
	
	*******************************
	**** Results summary **********
	*******************************
	
estimates table  log_diff_H log_diff_W* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  log_diff_H log_diff_W* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

estimates table  log_diff_H log_diff_O* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  log_diff_H log_diff_O* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

estimates table  ihs_diff_H ihs_diff_W* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  ihs_diff_H ihs_diff_W* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

estimates table  ihs_diff_H ihs_diff_O* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  ihs_diff_H ihs_diff_O* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)


log close 
clear 

/*
	** working paper versions. note: 9 would be mid-point 5 and 6, 11 would be mid-point 6 and 7. 
esttab ihs_fe2_B2 ihs_fe2_B5 ihs_fe2_B6 ihs_fe2_B7 ihs_fe2_S2 ihs_fe2_S5 ihs_fe2_S6 ihs_fe2_S7 using ihs_BS.tex, noconstant b(%9.4f) se(%9.3f) stats(r2) star(* 0.10 ** .05 *** .01) nogap mtitles title(Inverse Hyperbolic Sine: Beam and Fan table\label{ihs_BS}) replace

esttab log_fe2_C2 log_fe2_C5 log_fe2_C6 log_fe2_C7 log_fe2_H log_fe2_W10 log_fe2_R log_fe2_O10 using log_CG.tex, noconstant b(%9.4f) se(%9.3f) stats(r2) star(* 0.10 ** .05 *** .01) nogap mtitles title(Logarithm: Beam and Fan table\label{log_CG}) replace

esttab log_fe2_C2 log_fe2_C5 log_fe2_C6 log_fe2_C7 log_fe2_C2 log_fe2_C5 log_fe2_C6 log_fe2_C7  using log_Cx.tex, noconstant b(%9.4f) se(%9.3f) stats(r2) star(* 0.10 ** .05 *** .01) nogap mtitles title(Logarithm: Corners and Grid table\label{log_Cx}) replace

esttab ihs_fe2_C2 ihs_fe2_C5 ihs_fe2_C6 ihs_fe2_C7 ihs_fe2_C2 ihs_fe2_C5 ihs_fe2_C6 ihs_fe2_C7  using ihs_Cx.tex, noconstant b(%9.4f) se(%9.3f) stats(r2) star(* 0.10 ** .05 *** .01) nogap mtitles title(Inverse Hyperbolic Sine: Corners and Grid table\label{ihs_Cx}) replace
*/
